package com.ecoolex.ying.space.core.manager;

import com.ecoolex.ying.space.core.dao.OutsourcingPackagedGoodsDAO;
import com.ecoolex.ying.space.entity.OutsourcingPackaged;
import com.ecoolex.ying.space.entity.OutsourcingPackagedGoods;
import com.ecoolex.ying.space.entity.OutsourcingPackagedGoods.Column;
import com.ecoolex.ying.space.entity.OutsourcingPackagedGoodsExample;
import com.ecoolex.ying.space.entity.OutsourcingPackagedGoodsExample.Criteria;
import java.sql.Date;
import java.time.Instant;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OutsourcingPackagedGoodsManager {
	
    @Autowired
    private OutsourcingPackagedGoodsDAO dao;
    
    @Autowired
    private OutsourcingPackagedManager outsourcingPackagedManager;
    
    public void add(OutsourcingPackaged record,List<Integer> outsourcingGoodsIds, String createUser) {
    	for (int i=0;i< outsourcingGoodsIds.size();i++) {
			OutsourcingPackagedGoods outsourcingPackagedGoods = new OutsourcingPackagedGoods();
			outsourcingPackagedGoods.setOutsourcingPackagedId(record.getOutsourcingPackagedId());
			outsourcingPackagedGoods.setOutsourcingGoodsId(outsourcingGoodsIds.get(i));
			outsourcingPackagedGoods.setOrdered(i);
			outsourcingPackagedGoods.setDeleted(OutsourcingPackagedGoods.NOT_DELETED);
			outsourcingPackagedGoods.setCreateUser(createUser);
			outsourcingPackagedGoods.setCreateTime(Date.from(Instant.now()));
			outsourcingPackagedGoods.setUpdateUser(createUser);
			outsourcingPackagedGoods.setUpdateTime(Date.from(Instant.now()));

			dao.insert(outsourcingPackagedGoods);
		}
    }
    
    public void update(OutsourcingPackaged record,List<Integer> outsourcingGoodsIds, String updateUser) {
    	//删除原有商品
    	deleteByOutsourcingPackagedId(record,updateUser);
    	//添加修改后的商品
    	add(record,outsourcingGoodsIds,updateUser);
    }
    
    public void deleteByOutsourcingPackagedId(OutsourcingPackaged record, String updateUser) {
    	OutsourcingPackagedGoodsExample example = new OutsourcingPackagedGoodsExample();
    	Criteria criteria = example.createCriteria();
    	criteria.andOutsourcingPackagedIdEqualTo(record.getOutsourcingPackagedId()).andDeletedEqualTo(OutsourcingPackagedGoods.NOT_DELETED);
    	List<OutsourcingPackagedGoods> list = dao.selectByExample(example);
    	for (OutsourcingPackagedGoods outsourcingPackagedGoods : list) {
    		outsourcingPackagedGoods.setDeleted(OutsourcingPackagedGoods.IS_DELETED);
    		outsourcingPackagedGoods.setUpdateUser(updateUser);
    		outsourcingPackagedGoods.setUpdateTime(Date.from(Instant.now()));
    		dao.updateByPrimaryKeySelective(outsourcingPackagedGoods,Column.deleted,Column.updateTime,Column.updateUser);
		}
    }
    
    public boolean getByOutsourcingGoodsId(Integer outsourcingGoodsId) {
    	OutsourcingPackagedGoodsExample example = new OutsourcingPackagedGoodsExample();
    	Criteria criteria = example.createCriteria();
    	criteria.andOutsourcingGoodsIdEqualTo(outsourcingGoodsId).andDeletedEqualTo(OutsourcingPackagedGoods.NOT_DELETED);
    	List<OutsourcingPackagedGoods> packagedGoodsList = dao.selectByExample(example);
    	return outsourcingPackagedManager.getOnlineById(packagedGoodsList);
    }
    
    public List<OutsourcingPackagedGoods> getByOutsourcingPackagedId(Integer outsourcingPackagedId) {
    	OutsourcingPackagedGoodsExample example = new OutsourcingPackagedGoodsExample();
    	Criteria criteria = example.createCriteria();
    	criteria.andOutsourcingPackagedIdEqualTo(outsourcingPackagedId).andDeletedEqualTo(OutsourcingPackagedGoods.NOT_DELETED);
    	return dao.selectByExample(example);
    }
}